﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace admin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        private registration regForm;
        private bool OnlyFree; // только свободны номера
        private String szCheckIn;
        private String szCheckOut;
        private int selectedRoomId;
        private int selectedRoomCount;
        private String roomclass; // simple, lux etc.
        private RoomsView room;

        public MainWindow()
        {
            InitializeComponent();
            selectedRoomId = 0;
            selectedRoomCount = 0;
            roomclass = "";
            OnlyFree = false;
            try
            {
                roomType.ItemsSource = App.MyBase.GetRoomTypes();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Внимание! не удалось подключиться к БД.");
                Close();
            }
        }

        /// <summary>
        /// Заполнение таблицы номеров санатория
        /// </summary>
        private void UpdateRooms()
        {
            // проверить, установленны ли даты?
            if (dateCheckOut.Text == "" || dateCheckIn.Text == "" /*|| roomclass == ""*/)
                return;

            szCheckIn = dateCheckIn.Text;
            szCheckOut = dateCheckOut.Text;
            
            // показать только свободные номера
            if (OnlyFree == true)
            {
                clientsGrid.Items.Refresh();
                roomsGrid.ItemsSource = App.MyBase.GetFreeRooms(Convert.ToDateTime(szCheckIn), Convert.ToDateTime(szCheckOut), roomclass);
                //roomsGrid.ItemsSource = App.MyBase.GetRooms(dateCheckIn.DisplayDate, dateCheckOut.DisplayDate, !OnlyFree);
            }
            else // показать все
            {
                roomsGrid.ItemsSource = App.MyBase.GetRooms(Convert.ToDateTime(szCheckIn), Convert.ToDateTime(szCheckOut));
            }

            // send request
        }

        /// <summary>
        /// Регистрация клиента
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int occupate = 0;
            UpdateRooms();

            if (dateCheckOut.Text == "" || dateCheckIn.Text == "")
                return;

            // комната не выбрана
            if (selectedRoomId==0 || selectedRoomCount==0) 
                return;

            // проверка, на переполнение комнаты
            occupate = App.MyBase.GetFrClient(selectedRoomId, Convert.ToDateTime(szCheckIn),
                Convert.ToDateTime(Convert.ToDateTime(szCheckOut)));

            if (selectedRoomCount-occupate == 0)
            {
                MessageBox.Show("В выбранный номер заселение невозможно");
                return;
            }

            regForm = new registration(selectedRoomId);
            regForm.Show();
        }

        /// <summary>
        /// Закрытие приложения
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        // free rooms
        private void radioBtn1_Checked(object sender, RoutedEventArgs e)
        {
            OnlyFree = true;
            UpdateRooms();
        }

        // all rooms
        private void radioBtn0_Checked(object sender, RoutedEventArgs e)
        {
            OnlyFree = false;
            UpdateRooms();
        }

        private void dateCheckIn_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            szCheckIn = dateCheckIn.SelectedDate.ToString();
            UpdateRooms();
        }

        private void dateCheckOut_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            szCheckOut = dateCheckOut.SelectedDate.ToString();
            UpdateRooms();
        }

        /// <summary>
        /// Cделать выборку клиентов
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roomsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            room = (RoomsView)roomsGrid.SelectedItem;
            UpdateClients();
        }

        /// <summary>
        /// Процедура обновления данных в DataGrid
        /// </summary>
        private void UpdateClients()
        {
            if (room != null)
            {
                // показывать актуальную информацию
                selectedRoomId = room.RoomId;
                selectedRoomCount = room.Count;

                if (OnlyFree == false)
                { // все клиенты
                    //GetClients
                    clientsGrid.ItemsSource = App.MyBase.GetClients(room.RoomId, Convert.ToDateTime(szCheckIn),
                        Convert.ToDateTime(szCheckOut));
                }
                else
                { // free rooms
                    clientsGrid.Items.Refresh();
                }
            }
        }

        /// <summary>
        /// показать всех клиентов
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showAllClients_Click(object sender, RoutedEventArgs e)
        {
            clientsGrid.ItemsSource = App.MyBase.GetAllClients();
        }

        /// <summary>
        /// Включить сортировку номеров по типу
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roomType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            roomclass = (String)roomType.SelectedItem;
            UpdateRooms();
        }

        private void deleteClient_Click(object sender, RoutedEventArgs e)
        {
            Clients remove = (Clients)clientsGrid.SelectedItem;

            if (remove == null)
            {
                MessageBox.Show("Клиент не выбран!");
                return;
            }

            App.MyBase.RemoveClient(remove);
            clientsGrid.Items.Refresh();
        }

        private void outClient_Click(object sender, RoutedEventArgs e)
        {
            Clients remove = (Clients)clientsGrid.SelectedItem;

            if (remove == null)
            {
                MessageBox.Show("Клиент не выбран!");
                return;
            }

            App.MyBase.EscapeClient(remove, DateTime.Now);
            clientsGrid.Items.Refresh();
        }
    }
}
